﻿var XmlHttp;
function getXmlHttp()
{
    try
    {
            //Neu la trinh duyet Firefox, Opera 8.0 +, Safari  
            XmlHttp = new XMLHttpRequest();			
    }
    catch (e1)
    {
        // Neu la Internet Explorer 6.0+
        try
        {
                XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(e2)
        {
            try
            {
                //Neu la Internet Explorer 5.0+
                XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e3)
            {
                    alert("Trinh duyet cua ban khong ho tro Ajax (Your browser does not support Ajax)");
                    return false;
            }
        }
    }
}
function HandleResponse()
{
	if(XmlHttp.readyState == 4)
	{
		if(XmlHttp.status == 200)
		{		
                    ClearAndSetState(XmlHttp.responseText);
                  
		}
		else
		{
		    
			alert("Yeu cau cau ban khong thuc hien duoc" );                      
		}
	}	
}
function ClearAndSetState(text)
{
    document.getElementById("list").innerHTML = text;	
}
function Loading()
{
    document.getElementById("loading").style.display = 'block';
}
function Clear()
{
    document.getElementById("loading").style.display = 'none';
}

  /*----------------------------    */    
  function Search()
{    
    var rq = Math.random();
    var Name = document.getElementById("keyword1"); 
   
    if(Name.value != "")
    {                
        var requestUrl = 'Ajax.aspx?menu=search&prodName=' +encodeURIComponent(Name.value) + "&rq" + rq;                              
        getXmlHttp();	
        if(XmlHttp)
        {	
            XmlHttp.onreadystatechange = HandleResponse;		
            XmlHttp.open("GET", requestUrl,  true);		
            XmlHttp.send(null);		
        }
    }
}

function PageIndex(menu,subCate,p)
{   
    var requestUrl;
    requestUrl = "Ajax.aspx?menu="+menu+ "&p="+ p+"&idsubcat="+subCate;
	getXmlHttp();	
	if(XmlHttp)
	{	 
        XmlHttp.onreadystatechange = HandleResponse;
		XmlHttp.open("GET", requestUrl,true);
		XmlHttp.send(null);                
	}      
}
  
function GetMenu(menu)
{              
 
        var rq = Math.random();   
        var requestUrl;
            requestUrl = "Ajax.aspx?menu=" + encodeURIComponent(menu)+"&rq=" + rq;
	getXmlHttp();	
	if(XmlHttp)
	{	  
        XmlHttp.onreadystatechange = HandleResponse;
		XmlHttp.open("GET", requestUrl,true);
		XmlHttp.send(null);                
	}      
}
function GetListProductBySubCat(idCatSub) 
{            
        var requestUrl;
            requestUrl = "Ajax.aspx?menu=sp&idsubcat="+idCatSub;
	getXmlHttp();	
	if(XmlHttp)
	{	 
        XmlHttp.onreadystatechange = HandleResponse;
		XmlHttp.open("GET", requestUrl,true);
		XmlHttp.send(null);                
	}      
}
function GetListProductByCat(idCat) 
{            
 var rq = Math.random();
        var requestUrl;
            requestUrl = "Ajax.aspx?menu=sp&idcat="+idCat+"&rq=" + rq;
	getXmlHttp();	
	if(XmlHttp)
	{	 
        XmlHttp.onreadystatechange = HandleResponse;
		XmlHttp.open("GET", requestUrl,true);
		XmlHttp.send(null);                
	}      
}
function GetListCat(tabid,cat,page) 
{              
        var requestUrl;
            requestUrl = "Ajax.aspx?tabid=" + encodeURIComponent(tabid)+"&cat="+encodeURIComponent(cat)+"&page="+encodeURIComponent(page);
	getXmlHttp();	
	if(XmlHttp)
	{	  Loading();
        XmlHttp.onreadystatechange = HandleResponse;
		XmlHttp.open("GET", requestUrl,true);
		XmlHttp.send(null);                
	}      
}
function SellProduct(product,sell,tabid,page) //Bien viet
{              
        
        var requestUrl;
            requestUrl = "BuyAjax.aspx?productID=" + encodeURIComponent(product)+"&sell="+encodeURIComponent(sell)+"&tabid="+encodeURIComponent(tabid)+"&page="+encodeURIComponent(page);
	getXmlHttp();	
	if(XmlHttp)
	{	 
        Loading();
        XmlHttp.onreadystatechange = HandleResponse;
		XmlHttp.open("GET", requestUrl,true);
		XmlHttp.send(null);                
	}      
}
function GetProductDetailB(php,pid,tab)//Bien viet
{              
    var rq = Math.random();   
    var requestUrl;
        requestUrl = "BuyAjax.aspx?php=" + encodeURIComponent(php)+"&pid="+encodeURIComponent(pid)+"&cat="+tab+"&rq=" + rq;
            
	getXmlHttp();	
	if(XmlHttp)
	{	 
        Loading();
        XmlHttp.onreadystatechange = HandleResponse;
		XmlHttp.open("GET", requestUrl,true);
		XmlHttp.send(null);                
	}      
}
function GetProduct(tabid, cat,tab)
{              
    var rq = Math.random();   
    var requestUrl;
        requestUrl = "Ajax.aspx?tabid=" + encodeURIComponent(tabid)+"&cat="+encodeURIComponent(cat)+"&tab="+tab+"&rq=" + rq;
	getXmlHttp();	
	if(XmlHttp)
	{	 
        Loading();
        XmlHttp.onreadystatechange = HandleResponse;
		XmlHttp.open("GET", requestUrl,true);
		XmlHttp.send(null);                
	}      
}
function GetProductDetail(tabid,pid,tab)
{              
    var rq = Math.random();   
    var requestUrl;
        requestUrl = "Ajax.aspx?tabid=" + encodeURIComponent(tabid)+"&pid="+encodeURIComponent(pid)+"&tab="+tab+"&rq=" + rq;
            
	getXmlHttp();	
	if(XmlHttp)
	{	 
        Loading();
        XmlHttp.onreadystatechange = HandleResponse;
		XmlHttp.open("GET", requestUrl,true);
		XmlHttp.send(null);                
	}      
}
function Order(vl)
{       
    if(checkRegisterReal())
    {
        var frm = document;
        var cusName=""; var cusAdd="" ; var cusTel="";
        cusName = frm.getElementById("txtName").value;
        cusAdd = frm.getElementById("txtAdd").value;
        cusTel = frm.getElementById("txtTel").value;    
            var rq = Math.random();   
            var requestUrl;
                requestUrl = "Ajax.aspx?id=4&at=0&vl="+ encodeURIComponent(vl)+"&cusName="+ encodeURIComponent(cusName)+"&cusTel="+ encodeURIComponent(cusTel)+"&cusAdd="+ encodeURIComponent(cusAdd)+"&rq=" + rq;
	    getXmlHttp();	
	    if(XmlHttp)
	    {	 
            Loading();  					
            XmlHttp.onreadystatechange = HandleResponse;
		    XmlHttp.open("GET", requestUrl,true);
		    XmlHttp.send(null);                
	    }      
	}
}
function Contact(tabid)
{         
    var frm = document;
    var ctName=""; var ctAdd="" ; var ctTel=""; var ctSubject = ""; var ctEmail = "";var ctContent = "";
    ctName = frm.getElementById("txtName").value;
    ctAdd = frm.getElementById("txtAdd").value;
    ctTel = frm.getElementById("txtPhone").value;  
    ctEmail = frm.getElementById("txtEmail").value;  
    ctSubject = frm.getElementById("txtSubject").value;  
    ctContent = frm.getElementById("txtContent").value;  
    if(CheckInputContact())
    {        
        var rq = Math.random();   
        var requestUrl;
            requestUrl = "Ajax.aspx?tabid="+tabid+"&at=0&ctEmail="+ encodeURIComponent(ctEmail)+"&ctName="+ encodeURIComponent(ctName)+"&ctTel="+ encodeURIComponent(ctTel)+"&ctAdd="+ encodeURIComponent(ctAdd)+"&ctSubject="+ encodeURIComponent(ctSubject)+"&ctContent="+ encodeURIComponent(ctContent)+"&rq=" + rq;
	    getXmlHttp();	
	    if(XmlHttp)
    	{	 
        Loading();  					
        XmlHttp.onreadystatechange = HandleResponse;
		XmlHttp.open("GET", requestUrl,true);
		XmlHttp.send(null);                
	    }
	}      
}

function ViewCat(tabid, vl)
{   
        var rq = Math.random();   
        var requestUrl;
            requestUrl = "Ajax.aspx?tabid="+tabid+"&cat="+vl+"&rq=" + rq;
	getXmlHttp();	
	if(XmlHttp)
	{	 
        Loading();  					
        XmlHttp.onreadystatechange = HandleResponse;
		XmlHttp.open("GET", requestUrl,true);
		XmlHttp.send(null);                
	}      
}
function ViewNews(query)
{   
    var rq = Math.random();   
    var requestUrl;
        requestUrl = "Ajax.aspx?"+query+"&rq=" + rq;           
	getXmlHttp();	
	if(XmlHttp)
	{	 
        Loading();  					
        XmlHttp.onreadystatechange = HandleResponse;
		XmlHttp.open("GET", requestUrl,true);
		XmlHttp.send(null);                
	}      
}

function SearchAdvance(type)
{    
    var rq = Math.random();
    var keyWord22 = document.getElementById("keyWord22"); 
    var category =document.getElementById('category').value;   
    var manufacturer= document.getElementById('manufacturer').value;    
    var frommoney = document.getElementById("frommoney").value;    
    var tommoney = document.getElementById("tommoney").value;    
    if(type==2)
    {
        var requestUrl = 'Ajax.aspx?cat=16&keyWord22=' + keyWord22.value + '&category='+encodeURIComponent(category)+'&manufacturer='+encodeURIComponent(manufacturer)+'&frommoney='+encodeURIComponent(frommoney)+'&tommoney='+encodeURIComponent(tommoney)+"&rq" + rq;                              
        getXmlHttp();	
        if(XmlHttp)
        {	
            Loading();	
            XmlHttp.onreadystatechange = HandleResponse;		
            XmlHttp.open("GET", requestUrl,  true);		
            XmlHttp.send(null);		
        }
    }
}

function clearAll_Click()
{
	var numItems = document.forms[0].numItems.value;
	var check;
	for (var i = 1; i <= numItems; i++)
	{
		check = document.forms[0].item("check" + i);
		if (check.checked)
			check.click();
	}
}
function InsertCustomer(type)
{    
    var rq = Math.random();
    var Name1 = document.getElementById("txtFullName");
    var Name2 = document.getElementById("selectGender");
    var Name3 = document.getElementById("txtOffice");
    var Name4 = document.getElementById("txtPassword");
    var Name5 = document.getElementById("txtCofirm");
    var Name6 = document.getElementById("txtEmail");
    var Name7 = document.getElementById("txtAddress");
    var Name8 = document.getElementById("txtPhone");
    var Name9 = document.getElementById("txtFax");
    var Name10 = document.getElementById("selectQuestion");
    var Name11 = document.getElementById("txtAnswer");
        var requestUrl = 'Ajax.aspx?tabid=2&txtFullName=' + encodeURIComponent(Name1.value)+ '&selectGender=' + encodeURIComponent(Name2.value) + '&txtOffice=' + encodeURIComponent(Name3.value) + '&txtPassword=' + encodeURIComponent(Name4.value) + '&txtCofirm=' + encodeURIComponent(Name5.value) + '&txtEmail=' + encodeURIComponent(Name6.value) + '&txtAddress=' + encodeURIComponent(Name7.value) + '&txtPhone=' + encodeURIComponent(Name8.value) + '&txtFax=' + encodeURIComponent(Name9.value) + '&selectQuestion=' + encodeURIComponent(Name10.value) + '&txtAnswer=' + encodeURIComponent(Name11.value);                              
        getXmlHttp();	
        if(XmlHttp)
        {	
            Loading();	
            XmlHttp.onreadystatechange = HandleResponse;		
            XmlHttp.open("GET", requestUrl,  true);		
            XmlHttp.send(null);		
        }
  
}
function TestLogin(idProduct)
{       
    var frm = document;
    var Email=""; var pass="" ;var cbRemove=""; var txtQty="" ;
    var rq = Math.random();   
    var requestUrl;
        Email = frm.getElementById("txtUserName").value;
        Pass = frm.getElementById("txtPass").value;
        requestUrl = "Ajax.aspx?cat=9&UserName="+Email+"&Pass="+Pass+"&rq=" + rq;
    getXmlHttp();	
    if(XmlHttp)
    {	 
        Loading();  					
        XmlHttp.onreadystatechange = HandleResponse;
	    XmlHttp.open("GET", requestUrl,true);
	    XmlHttp.send(null);                
    }      
}
function openImageNews(vLink, vHeight, vWidth)
{
	var sLink = (typeof(vLink.href) == 'undefined') ? vLink : vLink.href;

	if (sLink == '')
	{
		return false;
	}

	winDef = 'status=no,resizable=yes,scrollbars=no,toolbar=no,location=no,fullscreen=no,titlebar=yes,height='.concat(vHeight).concat(',').concat('width=').concat(vWidth).concat(',');
	winDef = winDef.concat('top=').concat((screen.height - vHeight)/2).concat(',');
	winDef = winDef.concat('left=').concat((screen.width - vWidth)/2);
	newwin = open('', '_blank', winDef);

	newwin.document.writeln('<title></title><body topmargin="0" leftmargin="0" marginheight="0" marginwidth="0">');
	newwin.document.write('<a href="" onClick="window.close(); return false;"><img border=0 src=');
	newwin.document.write(sLink);
	newwin.document.write(' width=');
	newwin.document.write(vWidth);
	newwin.document.write(' height=');
	newwin.document.write(vHeight);
	newwin.document.write('></a>');
	newwin.document.write('</body>');

	if (typeof(vLink.href) != 'undefined')
	{
		return false;
	}
}



function InsertComment()
{    
    var rq = Math.random();
    var Name1 = document.getElementById("FullName");
    var Name2 = document.getElementById("Address");
    var Name3 = document.getElementById("Phone");
    var Name4 = document.getElementById("Email");
    var Name5 = document.getElementById("Titile");
    var Name = document.getElementById("Content1");
     var requestUrl ;
    if(Name.value != "")
    {    
        requestUrl = "Ajax.aspx?tabid=2&FullName=" + encodeURIComponent(Name1.value)+ "&Address=" +  encodeURIComponent(Name2.value) + '&Phone=' + Name3.value + '&Email=' + Name4.value + '&Title=' +  encodeURIComponent(Name5.value) + '&Content1='+  encodeURIComponent(Name.value)+"&cat=-6&rq=" + rq;                                 
    }
    else
    {
        requestUrl = "Ajax.aspx?tabid=2&cat=-6&rq=" + rq;    
    }
    getXmlHttp();	
    if(XmlHttp)
    {	
        Loading();	
        XmlHttp.onreadystatechange = HandleResponse;		
        XmlHttp.open("GET", requestUrl,  true);		
        XmlHttp.send(null);		
    }
    
}
// Bien viet
 function OpenWindow(theUrl,winName,features)
{
    window.open(theUrl+"&totalprice=ian3r9n03hltfa",winName,"width = 600px, height =650px, help=no, scroll=yes, status=no");                                 
} 
 function OpenWindowUploadFile(theUrl,winName,features)
{
    window.open(theUrl,winName,"width = 850px, height =650px, help=no, scroll=yes, status=no");                                 
} 
function CauHinhMay(categoryID,productID,productName,lblPrice, valuePrice)
{
    var temp = window.opener.document.getElementById("tongtien");
    var temp1 = window.opener.document.getElementById(lblPrice);
    var temp2=0;
    temp.value=temp.value.replace(/\./g,"");
    temp1.value=temp1.value.replace(/\./g,"");
    var so1=parseInt(temp.value);
    var so2=parseInt(temp1.value);
    if(so1>0)
    {
        temp2 = so1-so2;
    }
    window.opener.document.getElementById('show'+categoryID).value=productName;
    window.opener.document.getElementById(categoryID).value=productID;
    temp1.value=num2money(valuePrice).replace(".00","").replace(/,/g,".");
    var temptg=0;
    temptg=temp2+valuePrice;
    temp.value=num2money(temptg).replace(".00","").replace(/,/g,".");
    window.opener.document.getElementById('inPutDevToCart').style.display="block";
    window.opener.document.getElementById('DevSubmit').style.display="block";
    window.close();window.history.go(1);
}
function num2money(n_value) {
    // validate input
    if (isNaN(Number(n_value)))
    return 'ERROR';

    // save the sign
    var b_negative = Boolean(n_value < 0);
    n_value = Math.abs(n_value);

    // round to 1/100 precision, add ending zeroes if needed
    var s_result = String(Math.round(n_value * 1e2) % 1e2 + '00').substring(0, 2);

    // separate all orders
    var b_first = true;
    var s_subresult;
    while (n_value >= 1) {
    s_subresult = (n_value >= 1e3 ? '00' : '') + Math.floor(n_value % 1e3);
    s_result = s_subresult.slice(-3) + (b_first ? '.' : ',') + s_result;
    b_first = false;
    n_value = n_value / 1e3;
    }
    // add at least one integer digit
    if (b_first)
    s_result = '0.' + s_result;

    // apply formatting and return
    return b_negative
    ? '(' + s_result + ')'
    : s_result;
}
function UploadHinh(txtImage, img, path)
{
    window.opener.document.getElementById(txtImage).value=path;
    window.opener.document.getElementById(img).src=path;
    window.close();window.history.go(1);
}
function CheckCart(boolTest)
{
    if(boolTest==1)
    {
        alert("Giỏ hàng của bạn chưa có sản phẩm, vui lòng quay lai trang chủ để chọn");
        return false;
    }
}

function resetDev()
{
    document.getElementById('inPutDevToCart').style.display="none";
    document.getElementById('DevSubmit').style.display="none";
}

// show tin tuc - Linh Viet
function Show_Edu_News(count,index)
{   var check=document.getElementById(index).style.display;
    for (var i = 1; i <= count; i++)
	{
		 document.getElementById(i).style.display="none";
	}   
	if(check=="block")
	{
	    document.getElementById(index).style.display="none";
	}
	else
	{
	    document.getElementById(index).style.display="block";
	}
      
}